package PagePackage;




import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import static org.testng.AssertJUnit.*;

import org.openqa.selenium.interactions.Actions;

import LoginPackage.TestBaseClass;


public class usersPageClass extends internalPageClass {
	
	@FindBy (className="btn-add-new")
	public WebElement addNewUserButton;
	
	@FindBy (xpath="html/body/div/main/section/aside/form/div[1]/input")
	public WebElement userEmail;
	
	@FindBy (className="btn-search")
	public WebElement searchButton;
	
	@FindBy (xpath="html/body/div/main/section/aside/form/div[2]/input")
	public WebElement userFirstName;
	
	@FindBy (xpath="html/body/div/main/section/aside/form/div[3]/input")
	public WebElement userLastName;
	
	@FindBy (xpath="html/body/div/main/section/aside/form/div[4]/select")
	public WebElement userRole;
	
	@FindBy (xpath="html/body/div/main/section/aside/form/div[5]/select")
	public WebElement userStatus;
    
	public void findEmail(String email) {
		 driver.findElement(By.xpath("//*[contains(.,email)]"));
	}
	
	public void emailDoesNotExist(String cancelEmail) {
		 assertFalse(driver.findElement(By.xpath("//*")).getText().equals(cancelEmail));
			}
	
	
	public userContactInfoPageClass scrollAndClick( int x, int y, String emailRemove) {
		
		WebElement removeroles=driver.findElement(By.xpath(emailRemove));
		 
	    String code = "window.scroll(" + (removeroles.getLocation().x + x) + ","
	                                 + (removeroles.getLocation().y + y) + ");";
	 
	    ((JavascriptExecutor)driver).executeScript(code, removeroles, x, y);

	    WebElement edit2 = removeroles.findElement(By.xpath("../../td[2]/div/a"));
		 ((JavascriptExecutor) driver).executeScript("arguments[0].click();", edit2);
		 return MyPageFactory.getPage(driver, userContactInfoPageClass .class);
	}
public userContactInfoPageClass scrollAndClick3( int x, int y, String emailRemove) {
		
		WebElement removeroles=driver.findElement(By.xpath(emailRemove));
		 
	    String code = "window.scroll(" + (removeroles.getLocation().x + x) + ","
	                                 + (removeroles.getLocation().y + y) + ");";
	 
	    ((JavascriptExecutor)driver).executeScript(code, removeroles, x, y);  
	    
		 ((JavascriptExecutor) driver).executeScript("arguments[0].click();", removeroles);
		 return MyPageFactory.getPage(driver, userContactInfoPageClass .class);
	}
	
	
	public newUserPageClass scrollAndClick2(int x, int y, String userEmail) {
		 boolean present;
		    try {
		WebElement newUserClick=driver.findElement(By.xpath(userEmail));
		 
	  String code = "window.scroll(" + (newUserClick.getLocation().x + x) + ","
	                              + (newUserClick.getLocation().y + y) + ");";

	  ((JavascriptExecutor)driver).executeScript(code, newUserClick, x, y); 
	  
	      present = true;
	   } catch (NoSuchElementException e) {
	      present = false;
	   }
		    assertEquals(false, present);
		 return MyPageFactory.getPage(driver, newUserPageClass.class);
		 
	}
	public void clickTeamTab() {
		teamstab.click();
	}
	
	public usersPageClass clickUserTab() {
		userstab.click();
		return MyPageFactory.getPage(driver, usersPageClass.class);
	}
	
	public usersPageClass findUserByEmail(String email){
		userEmail.click();
		userEmail.clear();
		userEmail.sendKeys(email);
	
		return MyPageFactory.getPage(driver, usersPageClass.class);
	}
	public void openUser(String emailRemove)  {
		Actions actions = new Actions(driver);
		WebElement removeroles = driver.findElement(By.xpath(emailRemove));
		 
		actions.moveToElement(removeroles).perform();
		WebElement edit = removeroles.findElement(By.xpath("../td/button[contains(@class,'edit')]"));
		actions.moveToElement(edit).click().perform();
	}
	
	public usersPageClass findUserByFirstName(String firstName){
		userFirstName.click();
		userFirstName.clear();
		userFirstName.sendKeys(firstName);
		
		return MyPageFactory.getPage(driver, usersPageClass.class);
	}
	
	public usersPageClass findUserByLastName(String firstName){
		userLastName.click();
		userLastName.clear();
		userLastName.sendKeys(firstName);
		
		return MyPageFactory.getPage(driver, usersPageClass.class);
	}
	
	public usersPageClass findUserByRole(String role) {
		 Select select1 = new Select(userRole);
		 select1.selectByVisibleText(role);
		
		 return MyPageFactory.getPage(driver, usersPageClass.class);
	}
	
	public usersPageClass findUserByStatus(String status) {
		 Select select1 = new Select(userStatus);
		 select1.selectByVisibleText(status);
		 
		 return MyPageFactory.getPage(driver, usersPageClass.class);
	}
	public usersPageClass clickSearch() {
		searchButton.click();
		 return MyPageFactory.getPage(driver, usersPageClass.class);
			}
	
	public usersPageClass checkStatus(final String userstatus){
	
	Boolean until = new WebDriverWait( driver, 30 ).until( new ExpectedCondition<Boolean>()
	        {
	            public Boolean apply( WebDriver webDriver )
	            {
	                try
	                {
	                	String status = driver.findElement(By.xpath("html/body/div/main/section/div[1]/div/table/tbody/tr[3]/td[4]/div")).getText();
	            		assertEquals(status, userstatus);
	                	
	                  
	                }
	                catch( StaleElementReferenceException e )
	                {
	                	System.out.println( "Select failed! Try again..." );
	                    return false;
	                }
	                System.out.println( "Status found!" );
	                return true;
	                
	                
	            }
	
	
	        } );
	 return MyPageFactory.getPage(driver, usersPageClass.class);
	 }
	
	
	public usersPageClass checkMultiStatus(final String userMultistatus){
		
		Boolean until = new WebDriverWait( driver, 30 ).until( new ExpectedCondition<Boolean>()
		        {
		            public Boolean apply( WebDriver webDriver )
		            {
		                try
		                {
		           
		                	
		            		WebElement user = driver.findElement(By.xpath("//*[text()='victoria@victoria.com']"));
		            		String userStatus = user.findElement(By.xpath("../../td[4]/div")).getText();
		            		assertEquals(userStatus, userMultistatus);
		                }
		                catch( StaleElementReferenceException e )
		                {
		                	System.out.println( "Select failed! Try again..." );
		                    return false;
		                }
		                System.out.println( "Status found!" );
		                return true;
		                
		                
		            }
		
		
		        } );
	
	
	return MyPageFactory.getPage(driver, usersPageClass.class);
}
	
	public usersPageClass noUserResults(){
		
		Boolean until = new WebDriverWait( driver, 30 ).until( new ExpectedCondition<Boolean>()
		        {
		            public Boolean apply( WebDriver webDriver )
		            {
		                try
		                {
		                	String results= driver.findElement(By.xpath("html/body/div[1]/main/section/div[2]/h1")).getText();
		                	assertEquals("No Results Found", results);
		                	
		                  
		                }
		                catch( AssertionError e )
		                {
		                	System.out.println( "No message, trying again" );
		                    return false;
		                }
		                System.out.println( "Message is on the screen" );
		                return true;
		                
		                
		            }
		
		
		        } );
		 return MyPageFactory.getPage(driver, usersPageClass.class);}

	
	
	public boolean isOnThisPage(){
		return addNewUserButton.isDisplayed();
	}
	
	public userContactInfoPageClass clickonFirstResult(){
		driver.findElement(By.xpath("html/body/main/section/div[1]/div/table/tbody/tr[1]/td[1]/div/a")).click();
		return MyPageFactory.getPage(driver, userContactInfoPageClass.class);
		}
	
	
	
	 @Override
		void tryToOpen() {
		 MyPageFactory.getPage(driver, loginPageClass.class).loginAs("victoria@krossover.com", "webmaster");
		}

	
	

	
}
